package com.tongji.admin.view.compents
{
	import com.tongji.admin.vo.Picture;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/7/
	 */ 
	public class UploadCompMediator extends Mediator
	{
		[Inject]
		public var view:UploadComp;
		
		private var file:FileReference;
		private var request:URLRequest;
		
		public var picArray:ArrayCollection;
		
		override public function onRegister():void
		{
			picArray = new ArrayCollection();
			BindingUtils.bindProperty(view.picList, "dataProvider", this, "picArray");
			request = new URLRequest();
			file = new FileReference();
			file.addEventListener(Event.SELECT, selectFileHandler);
			file.addEventListener(Event.COMPLETE, onComplete);
			view.browserBtn.addEventListener(MouseEvent.CLICK, browserHandler);
		}
		
		private function browserHandler(event:MouseEvent):void
		{
			file.browse(getTypes());
		}
		
		private function getTypes():Array
		{
			var allTypes:Array = new Array(getImageTypeFilter());
			return allTypes;
		}
		
		private function getImageTypeFilter():FileFilter
		{
			return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
		}

		private function onComplete(event:Event):void
		{
			/*view.show.source = file.data;*/
		}
		
		public function selectFileHandler(event:Event):void
		{
			file = FileReference(event.target);
			var variables:URLVariables = new URLVariables();
			variables.fileName  = "caddy" + "_" + new Date().time + file.type;
			request.data = variables;
			file.load();
			
			var pic:Picture = new Picture();
			pic.pictureSrc = file.name + file.type;
			
			picArray.addItem(pic);
			//file.upload(request);			
		}  

	}
}